/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ita.controller;

import integration.BuscaPe.Buscape;
import integration.BuscaPe.BuscapeUrlJson;
import integration.BuscaPe.ConnectUrlJson;
import integration.BuscaPe.ProductEntityJson;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "product", urlPatterns = {"/product"})
public class ProductController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ConnectUrlJson conn = new ConnectUrlJson();
        String method = request.getParameter("method");
        String format = request.getParameter("format");
        if(method.equals(Methods.LIST.method()) && format.equals("json")){
            String url = request.getParameter("keyword");
            String urlCodificado = BuscapeUrlJson.SEARCH_PRODUCT_BY_KEYWORD.url() +URLEncoder.encode(url, "ISO-8859-1");
            response.getWriter().print(conn.getJsonFrom(urlCodificado));
        }

        
        //request.setAttribute("product", lista.get(0));
        //request.getRequestDispatcher("view/product.jsp").forward(request, response);
    
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }
}
